
TreeListener.prototype.onUnassert = function (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	if(!source.Value.match(/(urn:xmpp:roster:jid:)(.*)/))
		return;
	var id = RegExp.$2;
	var item = document.getElementById(id);
	if (item)
	{
		document.removeChild(item);
	}
}

TreeListener.prototype.onAssert = function (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	if(!source.Value.match(/(urn:xmpp:roster:jid:)(.*)/))
		return;
	var id = RegExp.$1;
	var status = rdf.GetResource("urn:xmpp:presence#status");
	var show = rdf.GetResource("urn:xmpp:presence#show"); 
	var item = document.getElementById(id);
	if (item)
	{
		item.setAttribute("show", getRDFLiteral(ds, source, show));
		item.setAttribute("status", getRDFLiteral(ds, source, status));
	}
}

Jazzila.prototype.onStateChange = function(state)
{
	if (state <= STATE_CONNECTED)
	{
//		purgeRoster();
	}
}

Jazzila.prototype.addUser =  function(jid, name, group){
	var roster = document.getElementById("roster-root");
	var grp_root =  roster;
	var usr_item = null;
	if (group)
		grp_root = roster.getElementById(group);
	usr_item = grp_root.getElementById("jid");
	if (!usr_item)
		usr_item = document.createElement("treeitem");
}

Jazzila.prototype.onVCard = function()
{
	var tree=document.getElementById("roster-tree");
	var jid = tree.view.getCellText(tree.currentIndex,"jid");
	this.getVCard(jid);
}

Jazzila.prototype.remove = function()
{
	var browser = CC["@mozilla.org/embedcomp/window-watcher;1"].getService(CI.nsIWindowWatcher);
	var menu  = document.getElementById("roster-context-menu");
	var jid  = menu.getAttribute("jid");
	this.nsIXmppService.unsubscribe(jid);
}

Jazzila.prototype.accept = function()
{
	var menu  = document.getElementById("roster-context-menu");
	var jid = menu.getAttribute("jid");
	this.nsIXmppService.allowSubscribtion(jid);
}

Jazzila.prototype.deny = function()
{
	var menu  = document.getElementById("roster-context-menu");
	var jid = menu.getAttribute("jid");
	this.nsIXmppService.denySubscribtion(jid);
}

Jazzila.prototype.onChatTo = function()
{
	var browser = CC["@mozilla.org/embedcomp/window-watcher;1"].getService(CI.nsIWindowWatcher);
	var menu  = document.getElementById("roster-context-menu");
	var to = menu.getAttribute("jid");
	var win = browser.getWindowByName(to, browser.activeWindow );
	if (!win)
	{
		win = browser.openWindow(null, "chrome://jazzila/content/chat.xul", to, "chrome", null);
		win.name = to;
	}
	win.focus();
}

Jazzila.prototype.onSearch = function()
{
	var browser = CC["@mozilla.org/embedcomp/window-watcher;1"].getService(CI.nsIWindowWatcher);
	var win = browser.getWindowByName("xmpp-search", browser.activeWindow );
	if (!win)
	{
		win = browser.openWindow(null, "chrome://jazzila/content/search.xul", "Search", "chrome", null);
		document.title = "Search";
		win.name = "xmpp-search";
	}
	win.focus();
}

Jazzila.prototype.onButtonClick = function (event)
{
	switch (event.button)
	{
	case 0:

		break;
	case 2:
		var jid = event.target.getAttribute("id");
		var menu = document.getElementById("roster-context-menu");
		var hidden = (event.target.getAttribute("show") != "waiting");
		menu.setAttribute("jid", jid);
		var fields = ["accept", "deny"];
		for (var i = 0; i< fields.length; i++ )
		{
			var m = document.getElementById(fields[i]);
			m.setAttribute("hidden", hidden);
		}
		this.showContextMenu(event.clientX, event.clientY, "roster-context-menu", "roster-tree");
	break;
	};
}


function createItemList(itr)
{
    var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
    var list = document.createElement("vbox");
    var name = rdf.GetResource("urn:xmpp:roster#name");
    var status = rdf.GetResource("urn:xmpp:presence#status");
    var show = rdf.GetResource("urn:xmpp:presence#show"); 
    while (itr.hasMoreElements())
    {
		var jid = itr.getNext().QueryInterface(CI.nsIRDFResource);
		var item = document.createElement("rosteritem");
		jid.Value.match(/(urn:xmpp:roster:jid:)(.*)/);
		item.setAttribute("id", RegExp.$2);
		item.setAttribute("name", getRDFLiteral(ds, jid, name));
		item.setAttribute("show", getRDFLiteral(ds, jid, show));
		item.setAttribute("status", getRDFLiteral(ds, jid, status));
		item.setAttribute("onclick", "jazzila.onButtonClick(event);");
		list.appendChild(item);
    }
    return list;
}

function createServerRow(txt)
{
    var hbox = document.createElement("rostergroup");
  
    hbox.setAttribute("value",txt);
    return hbox;
}

function onLoad()
{
	ds  = jazzila.getDataSource();
	var tbl = document.getElementById("roster-table");
	if(tbl)
	{
		var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
		var rdfCU = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
		var urn = rdf.GetResource("urn:xmpp:roster");
		var ctn = rdfCU.MakeBag(ds, urn);
		var etor = ctn.GetElements();
		var domain = rdf.GetResource("urn:xmpp:roster#domain");
		while (etor.hasMoreElements())
		{
		    var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
		    var row  = createServerRow(getRDFLiteral(ds, elem, domain));
		    tbl.appendChild(row);
		    var cts = rdfCU.MakeSeq(ds, elem);
		    row.appendChild(createItemList(cts.GetElements()));
		}
		jRL = new TreeListener();
		ds.AddObserver(jRL);
	}
}

function onUnload()
{
	if (ds)
		ds.RemoveObserver(jRL);
	delete jRL;
}

var jRL = null;
var ds = null;

window.addEventListener("load", onLoad, false);
window.addEventListener("unload", onUnload, false);
